<html>
    <head>
        <title>避免使用硬编码的字符串作为视图组件名称</title>
    </head>
    <body>
        <script>
            /*
                6、避免使用硬编码字符串

                                            为确保编译时安全性，请将硬编码的视图组件名称替换为类名。

                                            将 PriorityListViewComponent.cs 文件更新为不使用“ViewComponent”后缀：

                                                        using Microsoft.AspNetCore.Mvc;
                                                        using Microsoft.EntityFrameworkCore;
                                                        using ViewComponentSample.Models;

                                                        namespace ViewComponentSample.ViewComponents;

                                                        public class PriorityList : ViewComponent
                                                        {
                                                            private readonly ToDoContext db;

                                                            public PriorityList(ToDoContext context)
                                                            {
                                                                db = context;
                                                            }

                                                            public async Task<IViewComponentResult> InvokeAsync(
                                                                                                    int maxPriority, bool isDone)
                                                            {
                                                                var items = await GetItemsAsync(maxPriority, isDone);
                                                                return View(items);
                                                            }

                                                            private Task<List<TodoItem>> GetItemsAsync(int maxPriority, bool isDone)
                                                            {
                                                                return db!.ToDo!.Where(x => x.IsDone == isDone &&
                                                                                    x.Priority <= maxPriority).ToListAsync();
                                                            }
                                                        }


                                            视图文件：

                                                    <div>
                                                        Testing nameof(PriorityList) <br />

                                                        Maxium Priority: @ViewData["maxPriority"] <br />
                                                        Is Complete:  @ViewData["isDone"]
                                                        @await Component.InvokeAsync(nameof(PriorityList),  // 使用组件类名
                                                                        new { 
                                                                            maxPriority =  ViewData["maxPriority"],
                                                                            isDone = ViewData["isDone"]  }
                                                                        )
                                                    </div>

                                                    采用CLRLEI类型的 Component.InvokeAsync 方法重载使用 typeof 运算符：、

                                                       <div>
                                                            Testing typeof(PriorityList) <br />

                                                            Maxium Priority: @ViewData["maxPriority"] <br />
                                                            Is Complete:  @ViewData["isDone"]
                                                            @await Component.InvokeAsync(typeof(PriorityList),  // Type类型参数
                                                                            new { 
                                                                                maxPriority =  ViewData["maxPriority"],
                                                                                isDone = ViewData["isDone"]  }
                                                                            )
                                                        </div>

            */
        </script>
    </body>
</html>